package group2assignone.ucm.administrator;

import group2assignone.dao.MyDAO;
import group2assignone.func.Authentication;
import group2assignone.model.Manager;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.UITool;
import group2assignone.view.administrator.AddNewManagerView;

/**
 *public class UCSaveNewManager implements UCController
 * this is the class for the use case save new manager
 * it recieves the new manager view and the newly created manager model as
 * paramaters. it then adds the manager to the database
 * @author group2
 */
public class UCSaveNewManager implements UCController {

    private AddNewManagerView view;
    private boolean ok;
    private Authentication auth;
    private Manager man;

    public UCSaveNewManager(AddNewManagerView v) {
        view = v;
    }

    /**
     * Use Case: Save new manager
     * Pre-Condition: add new Manager UI
     * Post-Condition: UI is updated
     * 1. Customer clicks the save manager button
     * 2. System gathers the selected managers and adds it to the database
     * 3. system returns to the Admin main UI
     *
     * 2a. User entered invalid data, System shows error and prompts user to correct it
     */
    public void run() {
        auth = new Authentication();

        man = new Manager();
        view.upLoad(man);
        ok = auth.checkManagerFields(man);

        if (ok) {
            view.dispose();
            MyDAO coms = UCStartup.getInstance().getDAO();
            coms.sQLAddManager(man);
            UCUIAdminMainView uc = new UCUIAdminMainView();
            uc.run();
        } else {
            UITool.displayError(auth.getErrorMsg(), view);
        }

    }
}
